import 'package:flutter/material.dart';
import 'package:imove/pages/home/home_page.dart';
import 'package:imove/pages/movie/movie_page.dart';
import 'package:imove/pages/show/show_page.dart';
import 'package:imove/pages/tv/tv_page.dart';
import '../extension/icons_ext.dart';

class MainTabBar extends StatefulWidget {
  MainTabBar({Key key}) : super(key: key);

  @override
  _MainTabBarState createState() => _MainTabBarState();
}

class _MainTabBarState extends State<MainTabBar> {
  
  int seleted = 0;
  List<Widget> pages = [HomePage(),MoviePage(),ShowPage(),TVPage()];
  int _lastClickTime = 0;

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: _doubleExit,
      child: Scaffold(
        body: IndexedStack(
          index: seleted,
          children: pages,
        ),
        bottomNavigationBar: BottomAppBar(
          color: Colors.white,
          shape: CircularNotchedRectangle(),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            mainAxisSize: MainAxisSize.max,
            children: <Widget>[
              _bottomItem(0, '首页', Icons.home),
              _bottomItem(1, '电影', MyIcon.my_movie),
              _bottomItem(2, '综艺', MyIcon.my_show),
              _bottomItem(3, '电视剧', MyIcon.my_tv),
            ],          
          ),
        ), 
      )
    );
  }

  Widget _bottomItem(int index ,String title,IconData iconData){

    Color normalColor = Colors.grey;
    Color seletedColor = Colors.blue;

    return Expanded(
      child: SizedBox(
        height: 50,
        child: Padding(
          padding: EdgeInsets.only(top:6),
          child: GestureDetector(
            child: Column(
              children: <Widget>[
                Icon(iconData,color: index==seleted?seletedColor:normalColor,),
                Text(title,style: TextStyle(color: index==seleted?seletedColor:normalColor),)
              ],
            ),
            onTap: (){
              if(index == -1) return;
              setState(() {
                seleted = index;
              });
            },
          ),
        ),
      ),
    );
  }

  Future<bool> _doubleExit() {
    int nowTime = new DateTime.now().microsecondsSinceEpoch;
    if (_lastClickTime != 0 && nowTime - _lastClickTime > 1500) {
      return new Future.value(true);
    } else {
      // TheamToast.show('再按一次退出程序');
      _lastClickTime = new DateTime.now().microsecondsSinceEpoch;
      new Future.delayed(const Duration(milliseconds: 1500), () {
        _lastClickTime = 0;
      });
      return new Future.value(false);
    }
  }
}